package com.salon.modules.sys.service.impl;

import com.salon.common.utils.Constant;
import com.salon.modules.sys.entity.SysMenuEntity;
import com.salon.modules.sys.entity.SysUserEntity;
import com.salon.modules.sys.mapper.SysMenuMapper;
import com.salon.modules.sys.mapper.SysUserMapper;
import com.salon.modules.sys.service.IShiroService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class ShiroServiceImpl implements IShiroService {

    @Resource
    private SysMenuMapper sysMenuMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public Set<String> listsPermissionsByUserId(Long userId) {
        List<String> permsList;
        // 系统管理员，拥有最高权限
        if (userId == Constant.SUPER_ADMIN) {
            List<SysMenuEntity> menuList = sysMenuMapper.selectList(null);
            permsList = new ArrayList<>(menuList.size());
            for (SysMenuEntity menu : menuList) {
                permsList.add(menu.getPerms());
            }
        } else {
            permsList = sysUserMapper.listPermsByUserId(userId);
        }
        // 用户权限列表
        Set<String> permsSet = new HashSet<>();
        for (String perms : permsList) {
            if (StringUtils.isBlank(perms)) {
                continue;
            }
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }
        return permsSet;
    }

    @Override
    public SysUserEntity getUserById(Long userId) {
        return sysUserMapper.selectById(userId);
    }

}
